<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MigrationController
 *
 * @author Administrator
 */
class MigrationController extends PublicController
{
    
    private $cache;
    private $migrationsdb;

    public function __construct($data) {
        parent::__construct($data);
        $this->cache = $this->X('Cache');
        $this->migrationsdb = $this->X('Filedb', 'migrations');
    }
    
    public function project(){
        $data = $this->migrationsdb->getAll();
        $this->assign('data', $data);
        $this->display();
    }
    
    public function projectAdd(){
        $type = $this->g['type'];
        $this->assign('type', $type);
        $this->assign('oprName', '创建工程');
        $this->assign('projectId', uniqid());//生成项目标识
        $this->assign('isUpdate', 'false');
        $this->assign('postUrl', U('Tester/Migration/doProjectAdd'));
        $this->display();
    }
    
    public function doProjectAdd(){
        $project = $this->p;
        $key = md5(doEncrypt($project['projecttag'], __CODE_KEY__));
        if($this->migrationsdb->isExists($key)){die(json_encode(array('result' => false, 'msg' => '该工程已经存在')));}
        if($this->migrationsdb->set($key, $project)){
            die(json_encode(array('result' => true, 'msg' => '保存成功', 'returnUrl' => U('Tester/Migration/project'))));
        }
        die(json_encode(array('result' => false, 'msg' => '保存失败', 'returnUrl' => U('Tester/Migration/project'))));
    }
    
    public function projectUpdate(){
        $projecttag = $this->g['projecttag'];
        $key = md5(doEncrypt($projecttag, __CODE_KEY__));
        $data = $this->migrationsdb->get($key);
        $this->assign('data', $data);
        $this->assign('postUrl', U('Tester/Migration/doProjectUpdate'));
        $this->assign('isUpdate', 'true');
        $this->assign('oprName', '编辑项目');
        $this->assign('projectId', $projecttag);//生成项目标识
        $this->display('projectAdd');
    }
    
    public function doProjectUpdate(){
        $project = $this->p;
        $key = md5(doEncrypt($project['projecttag'], __CODE_KEY__));
        if($this->migrationsdb->set($key, $project)){
            die(json_encode(array('result' => true, 'msg' => '保存成功', 'returnUrl' => U('Tester/Migration/project'))));
        }
        die(json_encode(array('result' => false, 'msg' => '保存失败', 'returnUrl' => U('Tester/Migration/project'))));
    }
    
    public function projectOne(){
        $projecttag = $this->g['projecttag'];
        $key = md5(doEncrypt($projecttag, __CODE_KEY__));
        $data = $this->migrationsdb->get($key);
        $this->assign('data', $data);
//        $this->assign('postUrl', U('Tester/Migration/doProjectUpdate'));
//        $this->assign('isUpdate', 'true');
        $this->assign('oprName', '查看工程详情');
        $this->assign('projectId', $projecttag);//生成项目标识
        $this->display();
    }
}
